package com.example.thebasics;

import java.util.ArrayList;

import connection.ConnectionManager;

import data.Lista;
import data.Utilizador;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {
	 
	
	
	private ArrayList<String> updates = new ArrayList<String>();
	private ArrayAdapter<String> adaptador;
	private ListView listView;
	private ArrayList<Lista> listas;
	private Utilizador utilizador;
	//TextView textViewUtilizador;
	
	@SuppressWarnings("unchecked")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.inicial);
		listas = (ArrayList<Lista>) getIntent().getSerializableExtra("Listas");
		utilizador = (Utilizador) getIntent().getSerializableExtra("Utilizador");
		
		listView = (ListView) findViewById(R.id.listView1);
		adaptador = new ArrayAdapter<String>(this,android.R.layout.test_list_item, updates);
		listView.setAdapter(adaptador);
		listView.setOnItemClickListener(new OnClickListenerMainActivity(this));
		updateNotes();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	public void updateListaNotificacoes(){
		ConnectionManager connectionManager = new ConnectionManager();
		System.out.println("Lista de notificações a ser actualizada...");
		updates = connectionManager.actualizaListaNotificacoes(utilizador);
		if (updates != null) {
			for (String s : updates) {
				System.out.println(s);
			}
			System.out.println("Lista de Notificações Actualizada com Sucesso!");
			adaptador = new ArrayAdapter<String>(this,android.R.layout.test_list_item, updates);
			listView.setAdapter(adaptador);
			connectionManager.updateUtilizador(utilizador);
		}
		connectionManager.closeConnection();
	}
	 
	public void updateNotes(){
		System.out.println("NOTIFICACOES!!!!!");
		ConnectionMainTask cmt = new ConnectionMainTask(this);
		cmt.doInBackground("");

	}
	
	public void conf(View v){
		startActivity(new Intent(this, ConfigurationActivity.class));
	}

	public void lists(View v){
		Intent intent = new Intent(this, ListasActivity.class);
		intent.putExtra("Listas", listas);
		intent.putExtra("Utilizador", utilizador);
		startActivity(intent);
	}
	
	public void shop(View v){
		Intent intent = new Intent(this, ShopActivity.class);
		intent.putExtra("Listas", listas);
		intent.putExtra("Utilizador", utilizador);
		startActivity(intent);
	}
	 
	public void update(View v){
		updateNotes();
	}
	
	class OnClickListenerMainActivity implements OnItemClickListener{

		private MainActivity activity;
		
		public OnClickListenerMainActivity(MainActivity activity) {
			this.activity = activity;
		}
		
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			System.out.println("NOTIFICACOES!!!!!");
			ConnectionMainTask cmt = new ConnectionMainTask(activity);
			cmt.doInBackground("");
		}
		
	}
}

class ConnectionMainTask extends AsyncTask<String, Void, Boolean>{
	private MainActivity activity;
	
	public ConnectionMainTask(MainActivity activity) {
		this.activity = activity;
	}
	
	@Override
	protected Boolean doInBackground(String... params) {
		activity.updateListaNotificacoes();
        return false;
	}
}